/**
 * Copyright 2006 gworks.com.au
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. (http://www.apache.org/licenses/LICENSE-2.0)
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed 
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for 
 * the specific language governing permissions and limitations under the License.
 *
 * <dhamma-message src="Atisha (11th century Tibetan Buddhist master)">
 * 	The greatest achievement is selflessness. The greatest worth is self-mastery.
 * 	The greatest quality is seeking to serve others. The greatest precept is continual awareness.
 * 	The greatest medicine is the emptiness of everything. The greatest action is not conforming with the worlds ways.
 * 	The greatest magic is transmuting the passions. The greatest generosity is non-attachment.
 * 	The greatest goodness is a peaceful mind. The greatest patience is humility.
 * 	The greatest effort is not concerned with results. The greatest meditation is a mind that lets go.
 * 	The greatest wisdom is seeing through appearances. 
 * </dhamma-message>
 */
package au.com.gworks.jump.app.wiki.client;

import java.util.HashMap;
import java.util.Map;

import org.javaongems.client.Controller;
import org.javaongems.client.Gem;
import org.javaongems.client.macctrls.FishEyeBar;
import org.javaongems.client.macctrls.FishEyeItem;


import au.com.gworks.jump.app.wiki.client.administration.ControlPanel;
import au.com.gworks.jump.app.wiki.client.navigation.ExplorerController;
import au.com.gworks.jump.app.wiki.client.navigation.MyspacesController;

import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class ActionBar extends Composite implements Command {
	final static private String AUTH_TXT_P1 = "Show all spaces that the ";
	final static private String AUTH_TXT_P2 = " account is authorised to access";
	private DockPanel actionPane = new DockPanel();
	private FishEyeBar fishEyeMenu = new FishEyeBar(true);
	private FishEyeItem signInAction;
	
	private Button searchButton = new Button("<img src='" + Gem.stone.getApplImageCtx() + "search-small.png'>&nbsp;search");
	private TextBox	searchTextBox = new TextBox();
	private boolean authenticated;
	private boolean loaded;
	
	final static private String[][] FISH_EYE_ITEMS =
	{
		{"Login", Gem.stone.getApplImageCtx() + "sign-in.png"}
		, {AUTH_TXT_P1 + "guest" + AUTH_TXT_P2, Gem.stone.getApplImageCtx() + "myspaces.png"}
		, {"Setup sharing & security, groups, layouts using your moderation privileges", Gem.stone.getApplImageCtx() +"administer.png"}
		, {"Open the time machine to view the site back in time", Gem.stone.getApplImageCtx() + "time-machine.png"}
		, {"Create a permanent link that to a specific resource revision", Gem.stone.getApplImageCtx() + "permalink.png"}
		, {"Navigate up to the parent folder", Gem.stone.getApplImageCtx() + "up-one-level.png"}
		, {"Go to the home/root space in this space", Gem.stone.getApplImageCtx() + "home.png"}
		, {"Explore from the current location using a web file manager", Gem.stone.getApplImageCtx() + "explore.png"}
		, {"Open the online help system", Gem.stone.getApplImageCtx() + "help.png"}
	};
	
	public ActionBar() {
		setWidget(actionPane);
	}
	
	public void initialise() {
		actionPane.setVerticalAlignment(DockPanel.ALIGN_MIDDLE);
		actionPane.add(fishEyeMenu, DockPanel.WEST);
		actionPane.add(searchTextBox, DockPanel.WEST);
		actionPane.add(searchButton, DockPanel.WEST);
		for (int i = 0; i < FISH_EYE_ITEMS.length; i++) 
			fishEyeMenu.addItem(FISH_EYE_ITEMS[i][0], FISH_EYE_ITEMS[i][1], this);
		signInAction = fishEyeMenu.getItem(0);
		searchButton.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				execute();
			}
		});
	}
	
	public void load() {
		String authVal = Gem.stone.getGemPropertyAsString("isAuthenticated", null);
		authenticated = "true".equals(authVal); 
		if (authenticated) {
			String userName = Gem.stone.getGemPropertyAsString("userName", "guest");
			FishEyeItem itm =  fishEyeMenu.getItem(1);
			itm.setCaption(AUTH_TXT_P1 + userName + AUTH_TXT_P2);
			fishEyeMenu.removeItem(0);
		}
		loaded = true;
	}

	public void execute() {
		if (!loaded) {
			Gem.stone.reportStatus("All services are currently unavailable");
		} else {
			int sel = getSelectedFishEyeItemIdx();
			FishEyeItem itm = fishEyeMenu.getLastSelectedItem();
			if (itm == signInAction) { 
				String uri = Gem.stone.getGemPropertyAsString("toggleUriWithQs", null);
				Gem.stone.openUrl(uri);
			} else {
				switch (sel) {
				case 1: launchMySpaces(); break;
				case 2: launchControlPanel(); break;
				case 7: launchExplorer(); break;
				default: {
					String text = (itm == null) ? "nothing yet" : itm.getCaption();
					com.google.gwt.user.client.Window.alert("you clicked on:" + text);
				}
				}
			}
		}
	}
	
	public void launchExplorer() {
		ExplorerController ctrlr = new ExplorerController();
		Map map = getOpenWindowPosition();
		ctrlr.setUp(map);
		ctrlr.run();
	}
	
	public void launchMySpaces() {
		Controller ctrlr = (Controller) Gem.stone.getCargo().get(Jump.MYSPACES_CONTROLLER);
		if (ctrlr == null) {
			ctrlr = new MyspacesController();
			Map map = getOpenWindowPosition();
			ctrlr.setUp(map);
			Gem.stone.getCargo().put(Jump.MYSPACES_CONTROLLER, ctrlr);
		}
		ctrlr.run();
	}
	
	public void launchControlPanel() {
		ControlPanel.Controller ctrlr = new ControlPanel.Controller();
		Map map = getOpenWindowPosition();
		ctrlr.setUp(map);
	}

	private Map getOpenWindowPosition() {
		int[] sz = getExternWndLeftTopWidthHeight();
		Map map = new HashMap();
		map.put("top.left.width.height.array", sz);
		Gem.stone.getCargo().put("common-top.left.width.height.array", sz);
		return map;
	}
	
	private int[] getExternWndLeftTopWidthHeight() {
		int[] externWndLeftTopWidthHeight = new int[4];

		int height = getOffsetHeight();
		int width = getOffsetWidth();
		int left = getAbsoluteLeft();
		int top = getAbsoluteTop();
		int clientHeight = Window.getClientHeight();
		externWndLeftTopWidthHeight[0] = left;
		externWndLeftTopWidthHeight[1] = top + height;
		externWndLeftTopWidthHeight[2] = width;
		externWndLeftTopWidthHeight[3] = clientHeight - externWndLeftTopWidthHeight[1];
		return externWndLeftTopWidthHeight;
	}
	
	private int getSelectedFishEyeItemIdx() {
		int sel = fishEyeMenu.getLastSelectedItemIndex();
		if (authenticated)
			sel++;
		return sel;
	}
}
